-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Verify ownership of payment_source when creating WalletPaymentSource #3007
Verify ownership of payment_source when creating WalletPaymentSource #3007
Conversation
b66a8a6
to
749f9c5
Compare
|
||
validates_presence_of :user | ||
validates_presence_of :payment_source | ||
validates_presence_of :user |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer validates :user, presence: true
. Not a big deal though
validates_presence_of :payment_source | ||
validates_presence_of :user | ||
validates_presence_of :payment_source | ||
validates :user_id, uniqueness: { scope: [:payment_source_type, :payment_source_id] } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may be worth adding a custom message here. I found the default message on this is going to be User id has already been taken
. That message doesn't really denote that the violation is due to the tuple key
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, can we please document why this change is needed in this PR?
validates_presence_of :user | ||
validates_presence_of :payment_source | ||
validates :user_id, uniqueness: { scope: [:payment_source_type, :payment_source_id] } | ||
validate :check_for_payment_source_class |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm guessing the reasoning for this is to mimic what happens in the Spree::Wallet#add
method? I'm a fan prepending validation methods with validate
so you know the purpose of that method is to validate and if called will add to the object errors. validate_payment_source_class
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@skukx agree but these validations were already there, they have just been indented due to the module change. I think that if we want to change them we need to open another PR and deprecate existing methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @ericsaupe, I left some comments to improve PR documentation and also notice the build to fail on something that should be related to this, can you please take a look?
class Spree::WalletPaymentSource < ActiveRecord::Base | ||
belongs_to :user, class_name: Spree::UserClassHandle.new, foreign_key: 'user_id', inverse_of: :wallet_payment_sources | ||
belongs_to :payment_source, polymorphic: true, inverse_of: :wallet_payment_sources | ||
module Spree |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you please change the namespace/inheritance stuff into one or more other commits, so they are independently documented? Also, why are these change needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's to follow the format used in the other model files.
validates_presence_of :payment_source | ||
validates_presence_of :user | ||
validates_presence_of :payment_source | ||
validates :user_id, uniqueness: { scope: [:payment_source_type, :payment_source_id] } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, can we please document why this change is needed in this PR?
Without this validation it's possible for a user to add the same PaymentSource to their wallet multiple times causing dirty data and additional code in places where the wallet is displayed to remove duplicates.
This fixes the possibility of a User gaining access to a PaymentSource that isn't theirs by validating the WalletPaymentSource user_id is the same as the PaymentSource user_id if one is present.
To clarify some of these changes (each change should probably be documented in it's own commit): validates :user_id, uniqueness: { scope: [:payment_source_type, :payment_source_id] } This change is due to a unique tuple key on the database. Adding the validation mirrors what is enforced at the database layer and allows activerecord to handle it. What was a For validate :validate_payment_source_ownership It is possible that a user's wallet can have a payment source added to it which does not belong to the user. This means a user could make transactions on a payment source that does not belong to them. |
749f9c5
to
f4df47b
Compare
With the changes made to validate users cannot add payment sources from other users to their wallet. Some of our specs were using and adding payment sources created for other users to the wallet of the user being tested.
f4df47b
to
28c66c8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ericsaupe left a question, WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👏 thank you @ericsaupe
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a very good change. The error message could be improved, though.
@@ -488,6 +488,9 @@ en: | |||
attributes: | |||
payment_source: | |||
has_to_be_payment_source_class: has to be a Spree::PaymentSource | |||
not_owned_by_user: does not belong to user | |||
user_id: | |||
payment_source_already_exists: already has the Spree::PaymentSource in their wallet |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a very human friendly error message. Could we use
"already has this payment source in their wallet"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm assuming @ericsaupe took inspiration from the message above. I think we could fix both in this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am happy with this! 👍 Good work @ericsaupe!
This merge request takes care of these things:
spree_core
inheriting fromSpree::Base